﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.Windows.Forms;

namespace SmartMD.Manager.Report
{
    public class QRCode
    {
        public string encoding = "Byte";
        public int version = 7;
        public string correctionLevel = "H";
        public int txtSize = 2;

        public QRCode()
        { 
        }

        public Image Encode(string data)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            if (encoding == "Byte")
              {
                  qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
              }
              else if (encoding == "AlphaNumeric")
              {
                  qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
              }
              else if (encoding == "Numeric")
              {
                  qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
              }
              try
              {
                  int scale = Convert.ToInt16(txtSize);
                  qrCodeEncoder.QRCodeScale = scale;
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Invalid size!");
                  return null;
              }
              try
              {
                  qrCodeEncoder.QRCodeVersion = version;
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Invalid version !");
              }

              if (correctionLevel == "L")
                  qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
              else if (correctionLevel == "M")
                  qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
              else if (correctionLevel == "Q")
                  qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
              else if (correctionLevel == "H")
                  qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
              /*  
                 //设置编码类型
                 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                 //设置尺寸
                 qrCodeEncoder.QRCodeScale = 4;
                 //设置版本
                 qrCodeEncoder.QRCodeVersion = 7;
                 //设置纠错
                 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                * * */
           // data = "ThoughtWorks Interactive Technologies";
            Image image;
           /* if (QRCodeUtility.IsUniCode(data))
            {
                image = qrCodeEncoder.Encode(data, Encoding.GetEncoding("GB2312"));
            }
            else
            {
                image = qrCodeEncoder.Encode(data, Encoding.ASCII);
            }*/
            image = qrCodeEncoder.Encode(data.Trim());
            return image;

        }
       
        public string Decode(Image img)
        {
            QRCodeDecoder decoder = new QRCodeDecoder();

            String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(img)));

            return decodedString;
        }
    }
}
